package algorithm;

public class leetcode_0844 {
    public boolean backspaceCompare(String s, String t) {
        int lens=s.length();
        int lent=t.length();
        int current=0;
        int check=0;
        char [] stack=new char[Math.max(lens,lent)];
        int skip=0;
        for(int i=lens-1;i>=0;--i){
            char a=s.charAt(i);
            if(a=='#') skip++;
            else if(skip>0) skip--;
            else {
                stack[current]=a;
                current++;
            }
        }
        skip=0;
        for(int i=lent-1;i>=0;--i){
            char a=t.charAt(i);
            if(a=='#') skip++;
            else if(skip>0) skip--;
            else{
                if(stack[check]!=a) return false;
                check++;
            }
        }
        if(check==current) return true;
        else return false;
    }
}
